% Code generates all robustness checks in Table A.4

% 1) Baseline specification
% 2) No inattention
% 3) Discount factor = 0.99
% 4) Exclude gender norms from offer probability 
% 5) Add Education ^2 to belief specification 
% 6) No weight on u(T) in EV(T+1,.)
% 7) Estimate weight on u(T) in EV(T+1,.)
% 8) Let offer probability vary with age

clear; 
clc;

% loop over all robustness checks
for i =2:8 % enter the number of the robustness check (corresponding to the column of Table A4) here
             
        robustness_check=i

        % 0) import data inc set specification for preferences
        pref_spec=2; 
        close all;
        rng(88);
        
        % import bootstrap weights
        %---------------------------
        w_exante = csvread('../Data\created_data\xa_w.csv', 0,0);
        
        % import data - ex ante
        %-----------------------------
        [xadata] = import_and_setup_exante;
        
        % import and setup expost data
        %-----------------------------
        [xpdata,n_prefparms,n_groomchars] = import_and_setup_expost(pref_spec);
        
        % set up states
        % columns are: age, completed education, in school, current education (if
        % in school)
        %-----------------------------
        setup_states;
        
        
        % 3) Estimate overall beliefs
        
        load('../Output/main_estimates.mat')
        
         matvalues= ... % starting, lb, ub
            [   0.8,    0.1,    2; ...  % sigma - constant
                0,      0,      0; ...  % offer probability - het in age
                -3,     -8,     1; ...  % beliefs - constant
                0,      -1,     1; ...  % beliefs - age
                1.2,    -2,     3; ...  % beliefs - ed
                0,      0,      0; ...  % beliefs - ed^2   
                2,      -1,     5; ...  % beliefs - college
                -0.1,   -0.5,   0.5; ... % beliefs - age x ed
                -0.2,   -0.5,   0.5; ... % beliefs - bad girl multiplier
                0.3,    0,      0.5; ... % gamma
                -5,     -16,    -1; ...  % VTp1 
                1,      1,     1; ...    % weight on u0 in VTp1
                estimates.prefs(end),  estimates.prefs(end),   estimates.prefs(end)]    % inattentive share
        
        starting_vals=matvalues(:,1)';
        lb=matvalues(:,2)';
        ub=matvalues(:,3)';
        
        n_beliefparms=7;
        
        rng(1);
        
        w=ones(size(xadata.chosen_marry));
        
        fixedparms.n_prefparms=n_prefparms;
        fixedparms.n_beliefparms=n_beliefparms;
        fixedparms.n_groomchars=n_groomchars;
        fixedparms.ind_goodgirl=1;
        fixedparms.ind_likeschool=1;
        fixedparms.prefparms=estimates.prefs(end);
        fixedparms.disc=0.95;
        
        spec=2; % preference spec
        
        % 1) Baseline
        if robustness_check==1
        end
        
        % 2) No inattention
        if robustness_check==2
            starting_vals(end)=0;
            lb(end)=0;
            ub(end)=0;
            ub(1)=5; % shutting down inattention mechanically increases sigma
        end
        
        % 3) Discount factor = 0.99
        if robustness_check==3
            fixedparms.disc=0.99;
        end
        
        % 4) Exclude gender norms from offer probability 
        if robustness_check==4
            starting_vals(9)=0;
            lb(9)=0;
            ub(9)=0;
        end
        
        % 5) Add Education ^2 to belief specification 
        if robustness_check==5
            starting_vals(6)=0.01;
            lb(6)=0;
            ub(6)=1;
        end
        
        % 6) No weight on u(T) in EV(T+1,.)
        if robustness_check==6
            starting_vals(12)=0;
            lb(12)=0;
            ub(12)=0;
            ub(11)=5; % shutting down weight on u(T) means that mechanically the base psi will be higher
            starting_vals(11)=-1;
        end
        
        % 7) Estimate weight on u(T) in EV(T+1,.)
        if robustness_check==7
            starting_vals(12)=0.9;
            lb(12)=0;
            ub(12)=1;
        end
        
        % 8) Let offer probability vary by age
        if robustness_check==8
            starting_vals(2)=0;
            lb(2)=-0.2;
            ub(2)=0.2;
        end
        
        % 5) bootstrap beliefs
        % reload preference matrices
        temp=load('../Output/bs_results/bs_prefs.mat');
        bs_prefs=temp.bs_prefs;
        
        options = optimoptions('particleswarm','InitialSwarmMatrix',starting_vals,'UseParallel',true,'FunctionTolerance',1e-4,'OutputFcn',{@pigraph},'SwarmSize',100,'HybridFcn',@patternsearch,'Display','iter');
        nvars=size(lb,2);
        
        N=50;
        bs=ones(size(starting_vals',1),N);
        
        for n=0:N
            n
            w=w_exante(:,n+1);
            prefs_bs=bs_prefs(:,n+1)';
            fixedparms.prefparms=prefs_bs;
        
            rng(1);
            fun_bs=@(alpha0) aggregate_beliefs(states,xadata,fixedparms,spec,w,alpha0);
            [temp,f] = particleswarm(fun_bs,nvars,lb,ub,options)
            bs(:,n+1)=temp';
        end 
        
        se= std(bs(:,2:end),0,2);       
        real=bs(:,1);
        t=real./se;
        
        % TABLE 3: Structural Belief Parameters
        %--------------------------------------------------------------------------
         belief_results=[real se t]
         colNames = {'coef','se','t'}
         sTable = array2table(belief_results,'VariableNames',colNames) 
        
        hello= sprintf('../Output_New/robcheck_%0.1g.csv',robustness_check);
        writetable(sTable,hello)

        hello= sprintf('../Output_New/bs_results/bs_robcheck%0.1g.csv',robustness_check);
        csvwrite(hello,bs);


end
